package com.singzi.leetcode.N100;

public class T0005 {
    public static void main(String[] args) {
        String s = "aac";
        Solution0005 solution0005 = new Solution0005();
        System.out.println(solution0005.longestPalindrome(s));
    }
}
class Solution0005 {
    public String longestPalindrome(String s) {
        int n = s.length();
        if(n == 0 || n== 1) return s;
        int start = 0, left, end = 0, right = 0, max = 1,index = 0;
        while (index < n){
            for(int i = 0;i < 2;i++){
                left = index+i;
                right = index;
                while (left > 0 && right < n-1){
                    if(s.charAt(left-1) == s.charAt(right+1)){
                        left--;
                        right++;
                    }else break;
                }
                if(right > left && right - left + 1 > max) {
                    max =right - left + 1;
                    start = left;
                    end = right;
                }
            }
            index++;
        }
        return s.substring(start, end+1);
    }

}